#region File Header
//-----------------------------------------------------------------------------
// SibLib
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//
// This file is based on a XNAwiki sample.
//-----------------------------------------------------------------------------
#endregion

using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SibLib;

namespace SibLib.GameComponents
{
    /// <summary>
    /// A GameComponent which computes and displays Frames Per Second.
    /// </summary>
    public class FPSDrawer : DrawableGameComponent
    {
        private SpriteBatch spriteBatch;
        private SpriteFont videoFont;
        private float elapsedTime;
        private int totalFrames;
        private int fps;
        private string fontName;
        private Color textColor = Color.Red;
        private StringBuilder m_String;

        public string FontName
        {
            get { return fontName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    return;

                fontName = value;
                if (spriteBatch != null)
                    videoFont = Game.Content.Load<SpriteFont>(fontName);
            }
        }

        public Color Color
        {
            get { return textColor; }
            set { textColor = value; }
        }


        public FPSDrawer(Game game)
            : this(game, "SpriteFontDebug", true)
        {
        }

        public FPSDrawer(Game game, bool show)
            : this(game, "SpriteFontDebug", show)
        {
        }

        public FPSDrawer(Game game, string fontName)
            : this(game, fontName, true)
        {
        }

        public FPSDrawer(Game game, string fontName, bool show)
            : base(game)
        {
            this.fontName = fontName;
            Visible = show;
            DrawOrder = 10000;
            m_String = new StringBuilder(16);
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            // Load the font if fontName has been set.
            if (!string.IsNullOrEmpty(fontName))
                videoFont = Game.Content.Load<SpriteFont>(fontName);
        }

        public override void Update(GameTime gameTime)
        {
            elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            totalFrames++;

            if (elapsedTime >= 1.0f)
            {
                fps = totalFrames;
                totalFrames = 0;
                elapsedTime = 0;

                m_String.Remove(0, m_String.Length);
                m_String.Append("FPS: ");
                SibLib.Text.StringTools.AppendIntToStringBuilder(m_String, fps);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (videoFont != null)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(
                    videoFont,
                    m_String,
                    new Vector2(videoFont.LineSpacing),
                    textColor,
                    0f,
                    Vector2.Zero,
                    1f,
                    SpriteEffects.None,
                    0);
                spriteBatch.End();
            }
        }
    }

}